// Notice Celsius to Fahrenheit: F = C * 9/5 + 32

use std::env;
use std::process;

const FACTOR_DELTA: f32 = 9.0 / 5.0;
const FACTOR_BETA: f32 = 32.0;
const USAGE: &str = "usage: celsius_fahrenheit_converter <-c|-f> <temperature>";

fn main() {
    let args: Vec<String> = env::args().collect();
    if args.len() != 3 {
        println!("{}", USAGE);
        process::exit(1);
    }

    let temperature: f32 = match args[2].trim().parse() {
        Ok(num) => num,
        Err(_) => {
            println!("{}", USAGE);
            process::exit(1);
        }
    };
    
    if &args[1] == "-c" {
        println!("Fahrenheit to Celsius: {}ºF -> {}ºC", temperature, to_ceisius(temperature));
    }else if &args[1] == "-f" {
        println!("Celsius to Fahrenheit: {}ºC -> {}ºF", temperature, to_fahreheit(temperature));
    } else {
        println!("{}", USAGE);
        process::exit(1);
    }
}

fn to_ceisius(f: f32)->f32 {
    (f - FACTOR_BETA) / FACTOR_DELTA
}

fn to_fahreheit(c: f32)->f32 {
    c * FACTOR_DELTA + FACTOR_BETA
}
